<?php
class site_edit {
    var $db = null;
    var $smarty = null;
    var $msg = null;
	
	function site_edit() {
        $this->smarty = new MySmarty();
        $this->db = new DbAccess();
    }
	
	function displayForm($formvars = array())
	{
		$urllist_id = $formvars['blogId'];
		$sql = "SELECT * FROM urllist WHERE urllist_id = :urllist_id";
		$stmt = $this->db->prepare($sql);
		$stmt->bindParam(':urllist_id', $urllist_id);
		$stmt->execute();
		$result = $stmt->fetch();
		if($result != null)
		{
			$this->smarty->assign('rslt', $result);
		}
		else
		{
			$this->smarty->assign('fatal', true);
			$this->msg="ブログ[$urllist_id]は見つかりませんでした";
		}
		$stmt = null;
		$this->smarty->assign('formvars',$formvars);
		$this->smarty->assign('msg', $this->msg);
		$this->smarty->display( 'site_edit.tpl' );
	}
	
	function doInit($formvars = array())
	{
		$this->smarty->assign('formvars',$formvars);
		$this->smarty->assign('msg', $this->msg);
		$this->smarty->display( 'site_new.tpl');
		
	}
	
	function doUpdate($formvars = array())
	{
		$urllist_id = $formvars['blogId'];
		$sql = "Update urllist set blog_url='".$formvars['blog_url']."',"
		."blog_title='".$formvars['blog_title']."',rss_url='".$formvars['rss_url']."',"
		."open_flag=".$formvars['open_flag'].",country_id=".$formvars['country_id'].",lang_id=".$formvars['lang_id']
		.",crawler_rank=".$formvars['crawler_rank'].",enc='".$formvars['enc']."',last_update=NOW() 
		where urllist_id = $urllist_id";
	//	echo $sql ;
		$count =$this->db->exec($sql);
		if($count>0)
		{
			$this->msg = "ブログ[$urllist_id]の更新に成功しました";
		}
		else
		{
			$this->msg = "ブログ[$urllist_id]の更新に失敗しました";
		}
		
	}
	
	function doInsert($formvars = array())
	{
		$sql = "Insert into urllist(blog_url,blog_title,rss_url,open_flag,country_id,lang_id,crawler_rank,enc,last_update)
		        values('".$formvars['blog_url']."','".$formvars['blog_title']."','"
				.$formvars['rss_url']."',".$formvars['open_flag'].",".$formvars['country_id'].","
				.$formvars['lang_id'].",".$formvars['crawler_rank'].",'".$formvars['enc']."',NOW())";
				
	     //echo $sql;
		$count =$this->db->exec($sql);
		if($count>0)
		{
			$this->msg = "ブログを新規登録が完了しました。";
		}
		else
		{
			$this->msg = "ブログの新規登録ができません(ブログURLはすでに存在している可能性があります。)";
		}
		
	}
	
	function initDropDown()
	{
		$sql = "select country_id,country_name from country_mst";
		foreach ($this->db->query($sql) as $row) {
    		$resultArr[$row['country_id']]=$row['country_name'];
		}
		$this->smarty->assign('countryList', $resultArr);
		
		$sql = "select lang_id,lang_name from langMst where del_flag=0";
		foreach ($this->db->query($sql) as $row) {
    		$resultArr[$row['lang_id']]=$row['lang_name'];
		}
		
		$this->smarty->assign('langList', $resultArr);
		
		$this->smarty->assign('crawlerrankList', array(
                                1 => '週に１回',
                                2 => '数日に１回',
                                3 => '毎日'));
		
		$this->smarty->assign('statusList', array(
                                0 => 'CLOSE',
                                1 => 'OPEN'));
								
	    $this->smarty->assign('encList', array(
                                'UTF-8' => 'UTF-8',
                                'EUC-JP' => 'EUC-JP',
								'Shift-JIS' => 'Shift-JIS'));
	}	
}
?>